package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pagesize, count, list);
    }

    //修改视频喜欢的数据--马岩松
    @Override
    public void update(String likeId, int i) {
        //todo 检索字段设置错误--李磊//
        //Criteria criteria = Criteria.where("likeId").is(new ObjectId(likeId));
        Criteria criteria = Criteria.where("id").is(new ObjectId(likeId));
        Query query = Query.query(criteria);
        Update update = new Update();
        update.inc("likeCount", i);
        mongoTemplate.updateFirst(query, update, Video.class);
    }

    // 评论发布 -- 马岩松
    @Override
    public Integer saveVideos(Comment ma) {
        Video video = mongoTemplate.findById(ma.getPublishId(), Video.class);
        //2、向comment对象设置被评论人属性
        if (video != null) {
            ma.setPublishUserId(video.getUserId());
        }
        //3.保存到数据库
        mongoTemplate.save(ma);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(ma.getPublishId()));
        Update update = new Update();
        if (ma.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (ma.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);  //获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(ma.getCommentType());
    }
}
